package com.sugon.modules.report.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.sugon.dao.MesStoplineDao;
import com.sugon.modules.analyse.model.vo.DeviceKpiStatParamVO;
import com.sugon.modules.kpi.consts.DeviceOrganizationLevel;
import com.sugon.modules.kpi.model.StopLineStatVO;
import com.sugon.modules.kpi.model.dto.KpiDeviceDTO;
import com.sugon.modules.report.service.IStopLineOrDeviceManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ： YuXD
 * @description： 停线或设备停机Manager实现类
 * @date ： 2020-11-16 18:21
 * @version: 1.0
 */
@Service
public class StopLineOrDeviceManagerImpl implements IStopLineOrDeviceManager {

    @Autowired
    private MesStoplineDao mesStoplineDao;

    /**
     * doYearlyStopLineOrDeviceHourTotalStat
     *
     * @param kpiStatParam
     * @return
     */
    @Override
    public Map<Integer, StopLineStatVO> doYearlyStopLineOrDeviceHourTotalStat(DeviceKpiStatParamVO kpiStatParam) {
        List<StopLineStatVO> mttrRootData;
        if (kpiStatParam.getLevel().equals(DeviceOrganizationLevel.LINE)) {
            mttrRootData = mesStoplineDao.doMonthlyStopLineStat(kpiStatParam.getYearly(), kpiStatParam.getDeviceOrganizationCode());
        } else {
            mttrRootData = mesStoplineDao.doMonthlyStopDeviceStat(kpiStatParam.getYearly(), CollectionUtil.newArrayList(kpiStatParam.getDeviceOrganizationCode()));
        }
        if (mttrRootData == null) {
            mttrRootData = new ArrayList<>();
        }
        return mttrRootData.stream().collect(Collectors.toMap(StopLineStatVO::getMonth, account -> account));
    }

    /**
     * doYearlyFaultHourTotalStat
     *
     * @param kpiStatParam
     * @return
     */
    @Override
    public Map<Integer, StopLineStatVO> doYearlyFaultHourTotalStat(DeviceKpiStatParamVO kpiStatParam) {
        // 获取所有设备列表
        List<KpiDeviceDTO> kpiDeviceList = kpiStatParam.getDeviceIdLst();
        // 获取设备编码列表
        List<String> devCodeList = kpiDeviceList.stream().map(KpiDeviceDTO::getCode).collect(Collectors.toList());
        List<StopLineStatVO> mttrRootData = mesStoplineDao.doMonthlyStopDeviceStat(kpiStatParam.getYearly(), devCodeList);
        if (mttrRootData == null) {
            mttrRootData = new ArrayList<>();
        }
        return mttrRootData.stream().collect(Collectors.toMap(StopLineStatVO::getMonth, account -> account));
    }
}
